//
//  HZMOtherViewController.m
//  糖糖大魔王
//
//  Created by 韩震明 on 15/10/30.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "HZMOtherViewController.h"
#import "HZMOthersTVCell.h"

#define HEADW self.headView.frame.size.width
#define HEADH self.headView.frame.size.height

@interface HZMOtherViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) UIView *headView;
@property(nonatomic, retain) HZMOtherHeadView *header;
@property(nonatomic, retain) NSMutableArray *cellArr;
@property(nonatomic, retain) NSMutableArray *tagsArr;
@property(nonatomic, retain) NSMutableArray *picsArr;
@property(nonatomic, retain) HZMDynamic *dynamicDic;
@property(nonatomic, retain) HZMAuthor *authorDic;

@property(nonatomic, retain) NSMutableArray *earthArr;

    

@end

@implementation HZMOtherViewController


- (void)dealloc
{
    [_typeId release];
    [_tableView release];
    [_header release];
    [_cellArr release];
    [_headView release];
    [_picsArr release];
    [_dynamicDic release];
    [_authorDic release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setNavigation];
    
    
    [self createTableView];
    
    
    [self createData];
}



#pragma mark TableView Setting
- (void)createTableView
{
    if (self.navigationController.hidesBarsOnSwipe) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    } else {
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    }
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = self.view.frame.size.height;
    [self.tableView registerClass:[HZMOthersTVCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.separatorStyle = NO;
    [_tableView release];
}

#pragma mark TableView Delegate Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cellArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HZMOthersTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    cell.likesButton.tag = indexPath.row + 10000;
    
    if ([SaveTool ishaveCollected:self.cellArr[indexPath.row]]) {
        [cell.likesButton setImage:[UIImage imageNamed:@"No_Attention"] forState:UIControlStateNormal];
        cell.cellModel.liked = NO;
    }
    else
    {
        cell.cellModel.liked = YES;
        [cell.likesButton setImage:[UIImage imageNamed:@"Pay_Attention"] forState:UIControlStateNormal];
    }
    
    cell.cellModel = self.cellArr[indexPath.row];
    cell.picModel = self.picsArr[indexPath.row];
    cell.tagsModel = self.tagsArr[indexPath.row];
    cell.authorModel = self.authorDic;
    cell.dynamicModel = self.dynamicDic;
    return cell;
}





#pragma mark TableView Selected Method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HZMBaseWebView *webView = [[HZMBaseWebView alloc] init];
    webView.WebViewStrUrl = [self.cellArr[indexPath.row] share_url];
    webView.titleForWeb = @"好物详情";
    [self.navigationController pushViewController:webView animated:YES];
    [webView release];
}



#pragma mark Navigation Setting
- (void)setNavigation
{
    self.navigationItem.title = @"本期话题";
    
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    leftButton.frame = CGRectMake(0, 0, 40, 40);
    [leftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [self.view addSubview:rightButton];
    rightButton.frame = CGRectMake(0, 0, 40, 40);
    [rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];
    
    self.navigationController.navigationBar.translucent = NO;
    
}

#pragma mark Navigationbar ButtonAction
- (void)leftBarButtonClick:(UIBarButtonItem *)leftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightButtonClick:(UIButton *)rightButton
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56348b9b67e58ecc6300413f"
                                      shareText:@"我的"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, UMShareToTencent,UMShareToRenren, UMShareToDouban, UMShareToQzone, UMShareToEmail, UMShareToSms, UMShareToFacebook, UMShareToTwitter, nil]
                                       delegate:nil];

}



- (void)createData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *sd = [NSString stringWithFormat:@"%@", self.typeId];
    NSDictionary *bodyView = [NSDictionary dictionaryWithObjectsAndKeys:@"1445332782.662591", @"app_installtime", @"bt_app_ios", @"client_id", @"9c1e6634ce1c5098e056628cd66a17a5", @"client_secret", @"bf8be334eb1fd06b1bf65f5883dd415a", @"oauth_token", @"1242", @"screensize", sd, @"subject_id", @"0", @"page", @"10", @"pagesize", nil];
    NSDictionary *bodyTop = [NSDictionary dictionaryWithObjectsAndKeys:@"1445332782.662591", @"app_installtime", @"bt_app_ios", @"client_id", @"9c1e6634ce1c5098e056628cd66a17a5", @"client_secret", @"bf8be334eb1fd06b1bf65f5883dd415a", @"oauth_token", @"1242", @"screensize", sd, @"subject_id", nil];
    [manager POST:@"http://open3.bantangapp.com/community/subject/info" parameters:bodyTop success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject;
        self.header = [[[HZMOtherHeadView alloc] init] autorelease];
        [self.header setValuesForKeysWithDictionary:dic[@"data"][@"subject"]];
        if (!self.header) {
            [self createHeadView];
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        
    }];
    
    [manager POST:@"http://open3.bantangapp.com/community/subject/listPost" parameters:bodyView success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject;
        self.cellArr = [NSMutableArray array];
        self.cellArr = [HZMOtherView baseModelByArr:dic[@"data"][@"list"]];
        
        self.tagsArr = [NSMutableArray array];
        self.picsArr = [NSMutableArray array];
        self.dynamicDic = [[[HZMDynamic alloc] init] autorelease];
        self.authorDic = [[[HZMAuthor alloc] init] autorelease];
        
        for (NSInteger i = 0; i < self.cellArr.count; i++) {
            NSMutableArray *tagArr = [NSMutableArray array];
            tagArr = [HZMTags baseModelByArr:[self.cellArr[i] otherViewTags]];
            [self.tagsArr addObjectsFromArray:tagArr];
            NSMutableArray *picArr = [NSMutableArray array];
            picArr = [HZMPics baseModelByArr:[self.cellArr[i] pics]];
            [self.picsArr addObjectsFromArray:picArr];
            [self.dynamicDic setValuesForKeysWithDictionary:[self.cellArr[i] dynamic]];
            [self.authorDic setValuesForKeysWithDictionary:(NSMutableDictionary *)[self.cellArr[i] author]];
//            self.dynamicDic = [HZMDynamic b]
        }
        
        
        if ([self.cellArr count] > 0) {
            [self.tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        
    }];
    
}



#pragma HeadView
- (void)createHeadView
{
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 2)];
    self.tableView.tableHeaderView = self.headView;
    // 观察释放情况
    [_headView release];
    
    UIImageView *topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, HEADW, 7 * HEADH / 15)];
    [topImageView sd_setImageWithURL:[NSURL URLWithString:[self.header pic2]] placeholderImage:[UIImage imageNamed:@"back.gif"]];
    [self.headView addSubview:topImageView];
    [topImageView release];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 7 * HEADH / 15, HEADW - 10, HEADH / 6)];
    timeLabel.text = [self.header datestr];
    [self.headView addSubview:timeLabel];
    [timeLabel release];
    timeLabel.textColor = [UIColor grayColor];
    timeLabel.font = [UIFont systemFontOfSize:13.0];
    
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 9 * HEADH / 15, HEADW - 10, HEADH - HEADH / 6 - 7 * HEADH / 15)];
    descLabel.text = [self.header desc];
    descLabel.textColor = [UIColor grayColor];
    descLabel.font = [UIFont systemFontOfSize:13.0];
    descLabel.numberOfLines = 0;
    [descLabel sizeToFit];
    [self.headView addSubview:descLabel];
    [descLabel release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
